<template>
  <div class="app-container">
    <!-- 按钮组 -->
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-plus" plain @click="addRoleDialog">新增</el-button>
    </el-row>
    <!-- 数据展示 表单 -->
    <el-table v-loading="loading" :data="roleList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="角色编号" prop="id" />
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" />
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="editDialog(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="dataAuth(scope.row)"
          >数据权限</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- dialog -->
    <el-dialog
      title="添加角色"
      :visible.sync="addRoleVisible"
      width="30%"
    >
      <el-form ref="addRoleForm" label-width="80px" :model="addRoleForm" :rules="addRoleRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRoleForm.roleName" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限字符" prop="roleKey">
          <el-input v-model="addRoleForm.roleKey" size="small" placeholder="请输入权限字符" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addRoleVisible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="addRoleHandler">确 定</el-button>
      </span>
    </el-dialog>
<!--编辑角色-->
    <el-dialog
      title="编辑角色"
      :visible.sync="editRoleVisible"
      width="30%"
    >
      <el-form ref="editRoleForm" label-width="80px" :model="editRoleForm" :rules="addRoleRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editRoleForm.roleName" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限字符" prop="roleKey">
          <el-input v-model="editRoleForm.roleKey" size="small" placeholder="请输入权限字符" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="editRoleVisible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="editRoleHandler">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 角色权限dialog -->
    <el-dialog
      title="角色权限"
      :visible.sync="authUserVisible"
      width="30%"
    >
      <el-form ref="authUserFrom" label-width="80px" :model="authUserFrom" :rules="addRoleRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="authUserFrom.roleName" size="small" placeholder="请输入角色名称" disabled/>
        </el-form-item>
        <el-form-item label="权限字符" prop="roleKey">
          <el-input v-model="authUserFrom.roleKey" size="small" placeholder="请输入权限字符" disabled/>
        </el-form-item>
        <el-form-item label="数据权限" prop="data">
          <el-tree
            :data="authUserFrom.data"
            class="marTopStyle"
            show-checkbox
            node-key="id"
            ref="tree"
            :default-expanded-keys="[2, 3]"
            :default-checked-keys="authUserFrom.pitchKeys"
            :props="defaultProps"
          >
          </el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="authUserFrom = false">取 消</el-button>
        <el-button type="primary" size="small" @click="authUserHandler()">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import { getRole, delRole, updateRole, addRole, getRoleAuth, updateRoleAuth } from '@/api/role'
import { toTree } from '@/utils/common'
export default {
  name: 'Index',
  data() {
    return {
      // 搜索条件
      searchForm: {
        deptName: '',
        createTime: ''
      },
      // 表单数据
      roleList: [],
      // 表单遮罩层
      loading: true,
      // 添加角色的dialog
      addRoleVisible: false,
      // 添加角色的表单数据
      addRoleForm: {
        roleName: '',
        roleKey: 'ROLE_'
      },
      // 添加角色的表单验证规则
      addRoleRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '请输入角色字符', trigger: 'blur' }
        ]
      },
      // 编辑角色的dialog
      editRoleVisible: false,
      // 编辑角色的表单数据
      editRoleForm: {
        roleName: '',
        roleKey: '',
        id: ''
      },
      // 角色权限表单
      authUserFrom: {
        roleName: '',
        roleKey: '',
        data: [],
        id: '',
        // 拥有的权限id
        pitchKeys: []
      },
      // 角色权限的dialog
      authUserVisible: false,
      // 树形数据 children 子数 label标题
      defaultProps: {
        children: 'child',
        label: 'menuName'
      },
      // 存储权限获取的原始数据
      formerData: []
    }
  },
  created() {
    this.getRoleData()
  },
  methods: {
    // 打开添加角色的dialog
    addRoleDialog() {
      this.addRoleVisible = true
    },
    // 添加角色的按钮点击事件
    addRoleHandler() {
      this.$refs['addRoleForm'].validate((valid) => {
        if (valid) {
          addRole(this.addRoleForm).then(res => {
            this.addRoleVisible = false
            this.$message({ message: res.msg, type: 'success' })
            this.getRoleData()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取角色数据
    getRoleData() {
      getRole().then(res => {
        const { data } = res
        this.roleList = data
        this.loading = false
      })
    },
    // 删除用户
    handleDelete(data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRole(data.id).then(res => {
          const { msg } = res
          this.$message({
            type: 'success',
            message: msg
          })
          this.getRoleData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑执行
    editRoleHandler(data) {
      updateRole(this.editRoleForm).then(res => {
        this.$message({ message: res.msg, type: 'success' })
        this.editRoleVisible = false
        this.getRoleData()
      })
    },
    // 打开编辑框
    editDialog(data) {
      this.editRoleVisible = true
      this.editRoleForm.roleName = data.roleName
      this.editRoleForm.roleKey = data.roleKey
      this.editRoleForm.id = data.id
    },
    // 数据权限点击按钮
    dataAuth(data) {
      getRoleAuth().then(res => {
        this.formerData = res.data
        console.log(this.formerData)
        this.authUserFrom.data = toTree(this.formerData, 0)
      })
      this.authUserFrom.data.forEach(item => {
        this.authUserFrom.pitchKeys.push(item.id)
      })
      this.authUserVisible = true
      this.authUserFrom.roleName = data.roleName
      this.authUserFrom.roleKey = data.roleKey
      this.authUserFrom.id = data.id
    },
    // 数据权限提交数据
    authUserHandler() {
      // 获取选中id 过滤
      const authArry = []
      const len = this.formerData.length
      this.$refs.tree.getCheckedKeys().forEach(item => {
        for (let i = 0; i < len; i++) {
          if (item === this.formerData[i].id && this.formerData[i].menuType === 'F') {
            authArry.push(item)
          }
        }
      })
      updateRoleAuth({
        ids: authArry,
        roleId: this.authUserFrom.id
      })
      this.authUserVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
	.el-pagination{
		margin-top: 10px;
	}
  .marTopStyle{
    padding-top: 2%;
  }
</style>
